package logic

import (
	"context"
	"xialou_platform/app/errorx"
	"xialou_platform/services/address/model"
	"xialou_platform/services/address/rpc/internal/svc"
	"xialou_platform/services/address/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryCountyIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryCountyIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryCountyIdLogic {
	return &QueryCountyIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QueryCountyIdLogic) QueryCountyId(in *pb.IdBody) (*pb.AddressResp, error) {
	// todo: add your logic here and delete this line
	res, err := l.svcCtx.CountyModel.FindOne(in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, errorx.NewMsgCodeError(errorx.NotFoundResourceCode, "区县不存在")
		}
		return nil, errorx.NewCodeReason(errorx.DBErrorCode, err.Error())
	}
	resp := pb.AddressResp{
		Id:        res.Id,
		Name:      res.Name,
		Code:      res.CountyId,
		SuperCode: res.CityId,
	}
	return &resp, nil
}
